/* p7_1.c: A to D conversion */

/* This program converts the analog input from AIN0 (J3.9 of LaunchPad) using sample sequencer 3 and software trigger continuously.
Notice from Table 7-7, AIN0 channel is on PE3 pin. */

#include "TM4C123GH6PM.h"

int main(void)
{
    volatile int result;
	
    /* enable clocks */
    SYSCTL->RCGCGPIO |= 0x10;   /* enable clock to GPIOE (AIN0 is on PE3) */
    SYSCTL->RCGCADC |= 1;       /* enable clock to ADC0 */
    
    /* initialize PE3 for AIN0 input  */
    GPIOE->AFSEL |= 8;       /* enable alternate function */
    GPIOE->DEN &= ~8;        /* disable digital function */
    GPIOE->AMSEL |= 8;       /* enable analog function */
   
    /* initialize ADC0 */
    ADC0->ACTSS &= ~8;        /* disable SS3 during configuration */
    ADC0->EMUX &= ~0xF000;    /* software trigger conversion */
    ADC0->SSMUX3 = 0;         /* get input from channel 0 */
    ADC0->SSCTL3 |= 6;        /* take one sample at a time, set flag at 1st sample */
    ADC0->ACTSS |= 8;         /* enable ADC0 sequencer 3 */
    
    while(1)
    {
        ADC0->PSSI |= 8;        /* start a conversion sequence 3 */
        while((ADC0->RIS & 8) == 0) ;   /* wait for conversion complete */
        result = ADC0->SSFIFO3; /* read conversion result */
        ADC0->ISC = 8;          /* clear completion flag */
    }
}

void SystemInit(void)
{
    /* Grant coprocessor access */
    /* This is required since TM4C123G has a floating point coprocessor */
    SCB->CPACR |= 0x00f00000;
}